'use strict';

import * as actions from '../../actions/models';

// 对页面prop 数据进行管理
const initialState = {
	models: [],
	pageSize:10,
	totalModels:0,
	curPage:1,
	loginError:false,
	creating:false,
	error:false,
	message:"",
	moreModelsDialog:false,
	buyDialog:false,
	buyModelCount:0,
	shopModels:[],
	shopTotalModels:0,
	shopCurPage:1,
	shopPageSize:9,
	page_id:0
};
const defaultAction = {
  type: 'doNothing'
};

export default function index(state = initialState, action = defaultAction ) {

  switch (action.type) {

    case actions.GET_SHOP_MODELS_DONE:
		return Object.assign({}, state, {
			//models: [...state.models,...action.data]
			shopModels: action.data.data,
			shopTotalModels: parseInt(action.data.total),
			shopCurPage:action.params.page,
			shopPageSize:action.params.page_size,
			loginError:false,
			creating:false,
			error:false,
		});
	break;
    case actions.GET_MODELS_DONE:
		return Object.assign({}, state, {
			//models: [...state.models,...action.data]
			models: action.data.data,
			totalModels: parseInt(action.data.total),
			curPage:action.params.page,
			pageSize:action.params.page_size,
			moreModelsDialog:true,
			loginError:false,
			creating:false,
			error:false,
		});
	break;
    case actions.CREATE_PAGE_DONE:
		return Object.assign({}, state, {
			loginError:false,
			creating:false,
			error:false,
			page_id:action.page_id
		});
		break;
    case actions.CLOSE_DIALOG:
		return Object.assign({}, state, {
			loginError:false,
			creating:false,
			error:false
		});
		break;
    case actions.BUY_MODEL_DONE:
		return Object.assign({}, state, {
			loginError:false,
			creating:false,
			error:true,
			moreModelsDialog:true,
			buyDialog:false,
			buyModelCount:state.buyModelCount+1,
			message:action.message
		});
	break;
	case actions.SHOW_MODELS_DIALOG:
		return Object.assign({}, state, {
			moreModelsDialog:action.moreModelsDialog
		});
	break;
	case actions.SHOW_BUY_URL:
		return Object.assign({}, state, {
			buyDialog:action.buyDialog,
			buyUrl:action.url,
			creating:false,
			error:false,
			oid:action.oid
		});
	break;
    case actions.LOAD_START:
      return Object.assign({}, state, {
        loginError: false,
		message: "",
		error:false,
		creating:true,
      });
	  break;
    case actions.LOAD_ERR:
      return Object.assign({}, state, {
        loginError: action.loginFalse,
		message: action.message,
		error:true,
		creating:false
      });
	  break;
    default:
      return state;
  }
}

